<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          
          <el-form-item label="优惠券" prop="cid">
            <el-input v-model="form.cid" style="width: 370px;" />
          </el-form-item>




          <el-form-item label="关联商品" prop="goodsNames">
            <el-tag
              v-for="(goodsName,index) in form.nameList"
              :key="index"
              class="goodsTags"
            >{{ goodsName }}
            </el-tag>
            <!-- <el-input v-model="form.goodsNames" style="width: 370px;" /> -->
            <el-button icon="el-icon-s-grid" @click="openGoods">选择商品</el-button>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('id')" prop="id" label="ID" />
        <el-table-column v-if="columns.visible('cid')" prop="cid" label="优惠券" />
        <el-table-column v-if="columns.visible('productId')" prop="productId" label="商品" />
        <el-table-column v-if="columns.visible('addTime')" prop="addTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ formatTime(scope.row.addTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','yxStoreCouponProduct:edit','yxStoreCouponProduct:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />

      <choosegoods v-if="showGoods" @handleclose="cancelGoods" @confirmGoods="confirmGoods" />

    </div>
  </div>
</template>

<script>
import crudYxStoreCouponProduct from '@/api/yxStoreCouponProduct'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { formatTime } from '@/utils/index'
import choosegoods from '../../components/ChooseGoods'

// crud交由presenter持有
const defaultCrud = CRUD({ title: '商品优惠券', url: 'api/yxStoreCouponProduct', sort: 'id,desc', crudMethod: { ...crudYxStoreCouponProduct }})
const defaultForm = { id: null, cid: null, productId: null, addTime: null, isDel: null, goodsNames: '', nameList: [], goodsList: [] }
export default {
  name: 'YxStoreCouponProduct',
  components: { pagination, crudOperation, rrOperation, udOperation, choosegoods },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      permission: {
        add: ['admin', 'yxStoreCouponProduct:add'],
        edit: ['admin', 'yxStoreCouponProduct:edit'],
        del: ['admin', 'yxStoreCouponProduct:del']
      },
      rules: {
        cid: [
          { required: true, message: '优惠券不能为空', trigger: 'blur' }
        ],
        productId: [
          { required: true, message: '商品不能为空', trigger: 'blur' }
        ]
      },
      showGoods: false
    }
  },
  methods: {
    formatTime,
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    cancelGoods() {
      this.showGoods = false
    },
    openGoods() {
      this.showGoods = true
    },
    confirmGoods(goodsList = []) {
      this.showGoods = !this.showGoods
      if (goodsList && goodsList.length > 0) {
        const nameList = goodsList.map(item => { return item.storeName })
        this.form.nameList = nameList
        this.form.goodsNames = nameList.join(',')
        this.form.goodsList = goodsList
      }
    }
  }
}
</script>

<style scoped>
.goodsTags{
  margin-right: 10px;
}
</style>
